package com.web.controller.dormManagement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.apiresult.CommonResult;
import com.common.apiresult.ResultCode;
import com.common.domain.dto.WaterElectricityQueryDTO;
import com.common.domain.vo.WaterElectricityPageVO;
import com.common.domain.vo.WaterElectricityVO;
import com.service.serviceInterface.WaterElectricityService;
import com.service.utils.SecurityUtil;
import com.system.entity.Dorm;
import com.system.entity.WaterElectricity;
import com.system.mapper.WorkInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuxuhuang
 * @date 2023年04月04日 11:31
 * @desc 宿舍水电费控制器
 */
@RestController(value = "dormManagementWaterElectricityController")
@Api(value = "waterElectricity", tags = "宿舍水电费控制器")
@RequestMapping(value = "/dormManagement/waterElectricity")
@Slf4j
public class WaterElectricityController {
    @Resource
    private WorkInfoMapper workInfoMapper;
    @Resource
    private WaterElectricityService waterElectricityService;
    @Resource
    private SecurityUtil securityUtil;

    @GetMapping
    @ApiOperation(value = "获取所在楼宇宿舍水电费信息")
    public CommonResult get(WaterElectricityQueryDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        List<Dorm> dorms = workInfoMapper.selectDormIdAndNamesByUserId(userId);
        List<Long> ids = new ArrayList<>();
        for (Dorm dorm : dorms) {
            ids.add(dorm.getId());
        }
        List<WaterElectricity> list = waterElectricityService.list(new LambdaQueryWrapper<WaterElectricity>().in(WaterElectricity::getDormId, ids));
        List<WaterElectricity> collect = list.stream().skip(dto.getPageSize() * (dto.getPageNum() - 1)).limit(dto.getPageSize()).collect(Collectors.toList());
        List<WaterElectricityVO> vos = new ArrayList<>();
        for (WaterElectricity waterElectricity : collect) {
            WaterElectricityVO vo = new WaterElectricityVO();
            BeanUtils.copyProperties(waterElectricity, vo);
            dorms.stream().filter(j -> vo.getDormId().equals(j.getId().toString())).forEach(j -> vo.setDormName(j.getDormName()));
            vo.setAddName(workInfoMapper.selectNameByUserId(waterElectricity.getAddUserId()));
            vos.add(vo);
        }
        WaterElectricityPageVO pageVO = new WaterElectricityPageVO();
        pageVO.setVo(vos).setTotal(list.size());
        return list.size() > 0 ? CommonResult.success(pageVO) : CommonResult.failed(ResultCode.RETURNNULL);
    }
}
